<template>
  <el-card shadow="never" style="margin-top: 15px">
    <div class="head-container">
      <el-row>
        <el-tooltip class="item" effect="dark" content="返回" placement="right-start">
          <el-button type="text" @click="returnAdvertiserList" style="margin-left: 10px">
            <svg t="1708609271658" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"
                 p-id="5331" width="30" height="30">
              <path d="M257.2 567.1h584.5c29 0 52.5-23.4 52.5-52.2V512c0-28.8-23.5-52.2-52.5-52.2H255.9L431 284.1c20.2-20.3 20.2-53 0-73.3l-0.4-0.4c-20.2-20.3-53.1-20.5-73.6-0.5l-0.5 0.5L71.8 496.1a26.013 26.013 0 0 0 0 36.7l55.7 55.9L352 813.8c20.4 20.5 53.7 20.6 74.3 0.4l0.4-0.4 1.7-1.7c20.3-20.3 20.3-53.1 0-73.4L257.2 567.1z" p-id="5332" fill="#515151"></path>
            </svg>
          </el-button>
        </el-tooltip>
        <h1 class="row__title" style="margin-left: 20px;margin-top: 3px; justify-content: center;">编辑广告主</h1>
      </el-row>
    </div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px" class="demo-ruleForm" style="margin-top: 10px;margin-right: 10px">
      <el-form-item label="广告主编号：" prop="number">
        <el-input v-model="ruleForm.number" placeholder="请输入广告主编号"></el-input>
      </el-form-item>
      <el-form-item label="广告主名称：" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入广告主名称"></el-input>
      </el-form-item>
      <el-form-item label="联系方式：" prop="phone">
        <el-input v-model="ruleForm.phone" placeholder="请输入联系方式"></el-input>
      </el-form-item>
      <el-form-item label="邮箱：" prop="email">
        <el-input v-model="ruleForm.email" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item label="广告主公司：" prop="companyName">
        <el-input v-model="ruleForm.companyName" placeholder="请输入广告主公司"></el-input>
      </el-form-item>
      <el-form-item label="主要联系人：" prop="contactName">
        <el-input v-model="ruleForm.contactName" placeholder="请输入主要联系人"></el-input>
      </el-form-item>
      <el-form-item label="官方网站：" prop="website">
        <el-input v-model="ruleForm.website" placeholder="请输入官方网站"></el-input>
      </el-form-item>
      <el-form-item label="门店地址：" prop="storeAddress">
        <el-input v-model="ruleForm.storeAddress" placeholder="请输入门店地址"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitInfo()">提交编辑</el-button>
      </el-form-item>
    </el-form>
  </el-card>

</template>
<script>
import axios from "axios";
import {ElMessage} from "element-plus";
export default {
  data() {
    return {
      ruleForm: {
        number:'',
        name:'',
        phone:'',
        email:'',
        companyName:'',
        contactName:'',
        website:'',
        storeAddress:'',
        userId:''
      },
      rules: {
        number: [
          { required: true, message: '请输入广告主编号', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入广告主名称', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '电话号码不能为空' },
          { pattern:/^((0\d{2,3}-\d{7,8})|(1[34578]\d{9}))$/, message: '请输入正确的电话号码或者固话号码' , trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空' },
          {
            pattern: /^[a-zA-Z0-9]+([-_\.]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+\.[a-z]+$/,
            message: '请输入有效的邮箱地址', trigger: 'blur'
          }
        ],
        companyName: [
          { required: true, message: '请输入广告主公司', trigger: 'blur' },
        ],
        contactName: [
          { required: true, message: '请输入主要联系人', trigger: 'blur' }
        ],
        website: [
          { required: true, message: '请输入官方网站', trigger: 'blur' },
          { pattern: /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/,
            message: '请输入有效的网站' , trigger: 'blur' }
        ],
        storeAddress: [
          { required: true, message: '请输入门店地址', trigger: 'blur' }
        ],
      },
    }
  },
  methods: {
    returnAdvertiserList(){
      this.$router.push({path:'/rightManagement/AdvertiserManagement'})
    },
    async getAdvertiserDetail() {
      try {
        const res = await axios.post('/userManage/advertiserDetails/'+this.ruleForm.userId);
        if (res.status === 200) {
          this.ruleForm= res.data.data[0];
          console.log('数据', this.ruleForm);
        } else {
          ElMessage('获取广告主数据失败');
        }
      } catch (error) {
        console.error('Error fetching data:', error);
        ElMessage('获取广告主数据失败');
      }
    },
    async submitInfo(){
      try {
        const res = await axios.post('/userManage/updateAdvertiser',this.ruleForm);
        if (res.status === 200) {
          this.$message.success('更新广告商信息成功');
          this.getAdvertiserDetail();
        } else {
          this.$message.error('更新广告商信息失败');
        }
      }catch (error) {
        this.$message.warning('出错了');
      }
    }
  },
  created() {
    this.ruleForm.userId = this.list = this.$route.query.userId;
  },
  mounted() {
    this.getAdvertiserDetail();
  }
}
</script>